﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BaseData;
using BaseData.Common;
using TaxEntity.SysConfig;

namespace TaxData.SysConfig
{
    // <summary>
    /// 税种数据层
    /// </summary>
    public class TaxsData
    {
        /// <summary>
        /// 获取税种信息实体
        /// </summary>
        public TaxsEntity GetEntityById(int ID)
        {
            string sql = "select * from Taxs with(nolock) where ID = @ID and status=1";
            SqlParameter p = new SqlParameter("@ID", SqlDbType.Int, 8);
            p.Value = ID;
            DataTable dt = DataConfig.QueryRecords(BusinessType.DBRead, sql, p);
            TaxsEntity model = this.GetEntityHelper(dt);
            return model;
        }

        /// <summary>
        /// 获取税种列表
        /// </summary>
        public DataTable GetList()
        {
            string sql = "select * from Taxs with(nolock) where status=1 order by TaxType,Code";
            DataTable dt = DataConfig.QueryRecords(BusinessType.DBRead, sql);
            return dt;
        }

        /// <summary>
        /// 获取应税税种列表[暂时未用]
        /// </summary>
        public DataTable GetRequireList()
        {
            string sql = "select * from Taxs with(nolock) where status=1 and IsShow=1 order by TaxType,Code";
            DataTable dt = DataConfig.QueryRecords(BusinessType.DBRead, sql);
            return dt;
        }

        /// <summary>
        /// 删除税种
        /// </summary>
        public bool Delete(int ID)
        {
            string sql = "update Taxs set status=0 where ID = @ID";
            SqlParameter p = new SqlParameter("@ID", SqlDbType.Int, 8);
            p.Value = ID;
            int affectRows = DataConfig.QueryNoRecords(BusinessType.DBWrite, sql, p);
            return affectRows > 0;
        }

        /// <summary>
        /// 修改税种信息
        /// </summary>
        /// <param name="model">修改税种</param>
        /// <returns>操作结果</returns>
        public bool Update(TaxsEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Taxs set ");
            if (model.Code != null)
            {
                strSql.Append("Code='" + model.Code + "',");
            }
            if (model.Name != null)
            {
                strSql.Append("Name='" + model.Name + "',");
            }
            if (model.Rate != null)
            {
                strSql.Append("Rate=" + model.Rate + ",");
            }
            if (model.TaxType != null)
            {
                strSql.Append("TaxType='" + model.TaxType + "',");
            }
            if (model.IsShow != null)
            {
                strSql.Append("IsShow=" + model.IsShow + ",");
            }
            if (model.OptUserID != null)
            {
                strSql.Append("OptUserID=" + model.OptUserID + ",");
            }
            if (model.Status != null)
            {
                strSql.Append("Status=" + model.Status + ",");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where ID=" + model.ID + "");
            int rows = DataConfig.QueryNoRecords(BusinessType.DBWrite, strSql.ToString());
            return rows > 0;
        }

        /// <summary>
        /// 从DataTable中得到一个实体对象
        /// </summary>
        private TaxsEntity GetEntityHelper(DataTable dt)
        {
            TaxsEntity model = null;
            if (dt != null && dt.Rows.Count > 0)
            {
                model = new TaxsEntity();

                if (dt.Rows[0]["ID"] != null && dt.Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(dt.Rows[0]["ID"].ToString());
                }
                if (dt.Rows[0]["Code"] != null && dt.Rows[0]["Code"].ToString() != "")
                {
                    model.Code = dt.Rows[0]["Code"].ToString();
                }
                if (dt.Rows[0]["Name"] != null && dt.Rows[0]["Name"].ToString() != "")
                {
                    model.Name = dt.Rows[0]["Name"].ToString();
                }
                if (dt.Rows[0]["TaxType"] != null && dt.Rows[0]["TaxType"].ToString() != "")
                {
                    model.TaxType = dt.Rows[0]["TaxType"].ToString();
                }
                if (dt.Rows[0]["Rate"] != null && dt.Rows[0]["Rate"].ToString() != "")
                {
                    model.Rate = decimal.Parse(dt.Rows[0]["Rate"].ToString());
                }
                if (dt.Rows[0]["IsShow"] != null && dt.Rows[0]["IsShow"].ToString() != "")
                {
                    model.IsShow = int.Parse(dt.Rows[0]["IsShow"].ToString());
                }
                if (dt.Rows[0]["OptUserID"] != null && dt.Rows[0]["OptUserID"].ToString() != "")
                {
                    model.OptUserID = long.Parse(dt.Rows[0]["OptUserID"].ToString());
                }
                if (dt.Rows[0]["Status"] != null && dt.Rows[0]["Status"].ToString() != "")
                {
                    model.Status = int.Parse(dt.Rows[0]["Status"].ToString());
                }
            }
            return model;
        }
    }
}
